package com.zingrow.web.report.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.base.CodeMsg;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.base.enums.OperationDeed;
import com.zingrow.web.base.enums.OperationModule;
import com.zingrow.web.configure.enshrine.model.Enshrine;
import com.zingrow.web.report.model.RptEnshrine;
import com.zingrow.web.report.model.RptInfo;
import com.zingrow.web.report.service.IRptEnshrineService;
import com.zingrow.web.report.service.IRptInfoService;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.base.enums.OperationModule;
import com.zingrow.web.base.enums.OperationMinModule;
import com.zingrow.web.base.enums.OperationDeed;

@Controller
@RequestMapping(value = "/rpt/enshrine/", produces = "application/json;charset=UTF-8")
public class RptEnshrineController extends BaseController {
    @Autowired
    IRptEnshrineService enshrineService;
	@Autowired
	private IOptRecordService optRecordService;
    @Autowired
    IRptInfoService rptInfoService;
    
    /**
     * 查询已收藏信息列表
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public String selectAll(HttpServletRequest httpRequest, HttpServletResponse response, Integer pageIndex,
            Integer pageSize,Integer userId) {
    	List<RptEnshrine> list = new ArrayList<RptEnshrine>();
        ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
        		list);
        PageView pageView = null;
        try {
        	if (null==userId) {
        		standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
        	}
        	if (pageIndex == null || pageIndex <= 0 ) {
                pageIndex = 1;
            } 
            if (pageSize == null || pageSize <= 0) {
            	pageSize = 10;
    		}
            pageView = new PageView(pageSize,pageIndex);
            list = enshrineService.selectAll(pageView,userId);
        } catch (Exception e) {
            e.printStackTrace();
            standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
            standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
            log.error(e.getMessage());
        }
        standardData.setReturnData(list);
        standardData.setReturnCode(CodeMsg.CODE_OK);
        standardData.setReturnMessage(CodeMsg.MSG_OK);
        standardData.setTotalPages(pageView.getPageCount());
        standardData.setTotalRecords(pageView.getRowCount());
        return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 收藏与取消报告
     */
    @ResponseBody
    @RequestMapping("collectcancel")
    public String collectcancel(HttpServletRequest request, HttpServletResponse response, Integer ropertId,
            Integer userId,Integer isCollection,String operationId) {
        ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
                null);
        RptInfo rptInfo = rptInfoService.selectByRptInfoId(ropertId); 
        Integer cycleId=rptInfo.getCycleId();
        try {
        	//判断参数
            if (ropertId==null||userId==null||isCollection==null) {
            	standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				addOptRecode(request, optRecordService, "",
           			 "", "报告收藏取消收藏异常","参数异常");	
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
            }
            //初始化
            RptEnshrine enshrine = new RptEnshrine(ropertId,userId);
            String minChannel="";
            if(cycleId==1){
            	minChannel="日报";
            }
            if(cycleId==2){
            	minChannel="周报";
            }
            if(cycleId==3){
            	minChannel="月报";
            }    
            //isCollection 1收藏 0取消收藏
            if (isCollection==1) {
            	//收藏
            	if (enshrineService.insertEnshrine(enshrine)) {
            		standardData.setReturnCode(CodeMsg.CODE_OK);
                    standardData.setReturnMessage(CodeMsg.MSG_OK);                     
                    if(StringUtils.isNotBlank(operationId)){
                    if(operationId.equals("2")){
                    addOptRecode(request, optRecordService, OperationModule.me.toString(),
                    		OperationMinModule.collection.toString(), OperationDeed.collection.toString() + "《" + rptInfo.getName() + "》","");
                    }
                    if(operationId.equals("3")){
                    	 addOptRecode(request, optRecordService, OperationModule.me.toString(),
                         		OperationMinModule.history.toString(), OperationDeed.collection.toString() + "《" + rptInfo.getName() + "》","");
                    }
                    else if(operationId.equals("1")||operationId.equals("4")){
                    	 addOptRecode(request, optRecordService, OperationModule.report.toString(),
                    			 minChannel, OperationDeed.collection.toString() + "《" + rptInfo.getName() + "》","");	
                    }
                   }
				} else {
					standardData.setReturnCode(CodeMsg.CODE_ERROR);
	            	standardData.setReturnMessage("收藏失败");
				}
			} else {
				//取消收藏
				if (enshrineService.delEnshrine(userId,ropertId)) {
					standardData.setReturnCode(CodeMsg.CODE_OK);
                    standardData.setReturnMessage(CodeMsg.MSG_OK);
                    if(StringUtils.isNotBlank(operationId)){
                    if(operationId.equals("2")){
                        addOptRecode(request, optRecordService, OperationModule.me.toString(),
                        		OperationMinModule.collection.toString(), OperationDeed.nocollection.toString() + "《" + rptInfo.getName() + "》","");
                        }
                    if(operationId.equals("3")){
                    	addOptRecode(request, optRecordService, OperationModule.me.toString(),
                        		OperationMinModule.history.toString(), OperationDeed.nocollection.toString() + "《" + rptInfo.getName() + "》","");
                    }
                        else if(operationId.equals("1")||operationId.equals("4")){
                        	 addOptRecode(request, optRecordService, OperationModule.report.toString(),
                        			 minChannel, OperationDeed.nocollection.toString() + "《" + rptInfo.getName() + "》","");	
                        }
                    }
				} else {
					standardData.setReturnCode(CodeMsg.CODE_ERROR);
	            	standardData.setReturnMessage("取消失败");
				}
			}
        } catch (Exception e) {
            log.error("操作异常");
            log.error(e.getMessage());
            standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
            standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
        }
        return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
    }
   
    @ResponseBody
    @RequestMapping("queryByrptIduserId")
    public String queryByrptIduserId(HttpServletRequest httpRequest, HttpServletResponse response,Integer userId,Integer reportId) {
        ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
        		null);
        Enshrine enshrine = new Enshrine();
        try {
        	if (null==userId && null==reportId) {
        		standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
        	}
        	int isCollection = enshrineService.queryByrptIduserId(reportId, userId);
        	enshrine.setIsCollection(isCollection);
        } catch (Exception e) {
            e.printStackTrace();
            standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
            standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
            log.error(e.getMessage());
        }
        standardData.setReturnData(enshrine);
        standardData.setReturnCode(CodeMsg.CODE_OK);
        standardData.setReturnMessage(CodeMsg.MSG_OK);
        return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
    }
}
